package com.fresh.auth.controller;

import com.fresh.auth.Service.UserService;
import com.fresh.common.ResponseResult;
import com.fresh.entity.User;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;

    @PostMapping("/add")
    public Object add(@RequestBody User user){
        userService.add(user);
        return new ResponseResult(200,"ok",user);
    }

    @PostMapping("/delete")
    public void delete(@RequestBody User user){
        userService.delete(user);
    }

    @PostMapping("/update")
    public void update(@RequestBody User user){
        userService.updateN(user);
    }

    @GetMapping("/all")
    public Object all(Integer page){
        if (page == null) {
            page = 1;
        }
        User user = new User();
        PageInfo<User> pageInfo = userService.all(page,3);
        if(pageInfo == null){
            return new ResponseResult(200,"ok");
        }else{
            return new ResponseResult(200,"ok",pageInfo);
        }
    }

    @PostMapping("/findByCondition")
    public Object findByCondition(@RequestBody User user){
        PageInfo<User> pageInfo = userService.getPageByCondition(user,1,3);
        if(pageInfo == null){
            return new ResponseResult(200,"ok");
        }else{
            return new ResponseResult(200,"ok",pageInfo);
        }
    }
}
